﻿
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Web;
using System.Web.Security;
using Core.Library;
using DataObject;
using DataObject.Members;
using DataObject.Ref;

namespace Event
{
    public partial class LoginRegister : LoginMing
    {
        protected override void getData(string url)
        {
            try
            {
                string result = string.Empty;

                WebRequest wrq = WebRequest.Create(url);
                WebResponse wrs = wrq.GetResponse();

                StreamReader strdr = new StreamReader(wrs.GetResponseStream(), Encoding.UTF8);
                result = strdr.ReadToEnd();
                strdr.Close();
                wrs.Close();
                var o = VietIDObject.ConvertMingJson(result);
                var UDisplay = string.Empty;
                var ULink = string.Empty;
                string UClick = string.Empty;
                string UrlRound = "#";
                if (!String.IsNullOrEmpty(o.user.email))
                {
                    MembersObject u = MembersCache.Members_GetByEmail(o.user.email);
                    if (u == null)
                    {
                        u = new MembersObject();
                        u.Member_Name = o.user.username;
                        u.Member_Email = o.user.email;
                        u.Member_Status = (int)MemberStatus.IsNoRegister;
                        u.Member_InRound = (int)MemberRound.NoRound;
                        u.Member_Birthday = DateTimeFunctions.ValidDateTime(o.user.birthday);
                        u.Member_RegisterDate = DateTime.Now;
                        u.Member_ID = MembersController.Members_Insert(u);
                    }
                    o.user.status = u.Member_Status;
                    o.user.round = u.Member_InRound;
                    o.user.memberId = u.Member_ID;

                    if (u.Member_Status == (int)MemberStatus.IsPublished && RefCache.Ref_GetLockEditStatus() && u.Member_InRound >= RefCache.Ref_GetCurrentRound())
                    {
                        UDisplay = "Sửa thông tin";
                        ULink = "/sua-thong-tin.htm";
                        // chua dc set vong thi UrlRound ve vong 1
                    }
                    else if (u.Member_Status == (int)MemberStatus.IsNoRegister && RefCache.Ref_GetLockRegisterStatus())
                    {
                        UDisplay = "Đăng ký dự thi";
                        ULink = "/dang-ky-du-thi.htm";
                    }
                    if (u.Member_Status == (int)MemberStatus.IsPublished)
                    {
                        if (u.Member_InRound == (int)MemberRound.NoRound)
                            u.Member_InRound = (int)MemberRound.Round1;
                        UrlRound = UrlHelpers.MembersDetails(u.Member_ID, u.Member_Email, u.Member_InRound);
                    }
                    //
                }
                string userDataString = o.user.username + "|" + o.user.email + "|" + o.user.full_name + "|" + o.user.status + "|" + o.user.round + "|" + o.user.memberId;
                HttpCookie authCookie = FormsAuthentication.GetAuthCookie(o.user.username, true);
                FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(authCookie.Value);
                FormsAuthenticationTicket newTicket = new FormsAuthenticationTicket(ticket.Version, ticket.Name, DateTime.Now, DateTime.Now.AddMonths(1), ticket.IsPersistent, userDataString);
                authCookie.Value = FormsAuthentication.Encrypt(newTicket);
                Response.Cookies.Add(authCookie);


                // luu vao session
                HttpContext.Current.Session[Const.AuthenticationSessionName] = userDataString;
                // luu vao session

                if (o.user.status == (int)MemberStatus.IsPublished) Response.Write("<script>parent.location.href = \"/sua-thong-tin.htm\";</script>");
                else Response.Write("<script>parent.location.href = \"/dang-ky-du-thi.htm\";</script>");
            }
            catch
            {
                Response.Write("<script>parent.reload();</script>");
            }
        }
    }
}